﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using EasyLibrary.AR.Classes;

namespace EasyFrota_Mobile
{
    public partial class Rotas : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {

                IList RotasList = new ArrayList();
                var r = new Rota();
                RotasList = r.LoadAll();
                DropDownAdd(RotasList);
                DropDownCompoeHoras();

                
            }
          

        }

        private void DropDownCompoeHoras()
        {
           
            DropDownHora.Items.Add(new ListItem(DateTime.Now.Hour.ToString(),DateTime.Now.Hour.ToString()));
            for(int  i=0;i<24;i++)
            {
               DropDownHora.Items.Add(new ListItem(i.ToString(),i.ToString()));
            }

            DropDownMinuto.Items.Add(new ListItem(DateTime.Now.Minute.ToString(), DateTime.Now.Minute.ToString()));
            for (int i = 0; i < 60; i++)
            {
                DropDownMinuto.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
        }

        private void DropDownAdd(IList rotasList)
        {
            dropDownRota.Items.Add(new ListItem("Selecione", "0"));
            foreach (Rota route in rotasList)
            {
                dropDownRota.Items.Add(new ListItem(route.Nome, route.IDRota.ToString()));
            }
        }


        protected void dropDownRota_SelectedIndexChanged(object sender, EventArgs e)
        {
            IList veiList = new ArrayList();
            var v = new Viatura();
            veiList = v.getViaturaByRota(Convert.ToInt32(dropDownRota.SelectedValue));

            dropDownVeiculos.Items.Add(new ListItem("Selecione", "0"));
            foreach (Viatura via in veiList)
            {
                dropDownVeiculos.Items.Add(new ListItem(via.Matricula, via.Idviatura.ToString()));
            }

        //  HtmlGenericControl body = (HtmlGenericControl)Page.Master.FindControl("mobileBody");
       //   body.Attributes.Add("onload", "initialize();");
        }

        protected void dropDownVeiculos_SelectedIndexChanged(object sender, EventArgs e)
        {
          RotaViatura rv=new RotaViatura();
            int idRota = Convert.ToInt32(dropDownRota.SelectedValue);
            int idViatura = Convert.ToInt32(dropDownVeiculos.SelectedValue);
            int idrv;
            idrv = rv.getIDRotaViaturaByRotaAndViatura(idRota, idViatura);
            HiddenField1.Value = idrv.ToString();
            HtmlGenericControl body = (HtmlGenericControl)Page.Master.FindControl("mobileBody");
            body.Attributes.Add("onload", "initialize();");
        }
    }
}